$(document).ready(function () {
	    var areaCode=$("#areaCode").val();
 		var arr = area();
		for(var i=0; i<arr.length;i++){
			$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
		}	
		if(areaCode==undefined || areaCode==""){
			areaCode=$("#change").find("option:selected").val();
		}
		loadData(areaCode);
		$("#change").change(function () {
			areaCode=$("#change").find("option:selected").val();
       		loadData(areaCode);
   		});	
});


function loadData(areaCode){
		$.ajax({
            url: urlcore + "/api/sendTask/sendTaskDetailsByAreaCode?areaCode="+areaCode,
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                  da = data.data;
                  if(da==null){ 
					  $("#restDays").val("");
		              $("#dailyTasks").val("");
		              $("#orderTicket").val("");
		              $("#worktimeMon").val("");
		              $("#worktimeEve").val("");
		              $("#changeDays").val("");
                  }else{
                 	  $("#change").find("option[value='"+da.areaCode+"']").attr("selected",true);
		              $("#restDays").val(da.restDays);
		              $("#dailyTasks").val(da.dailyTasks);
		              $("#orderTicket").val(da.orderTicket);
		              $("#worktimeMon").val(da.worktimeMon);
		              $("#worktimeEve").val(da.worktimeEve);
		              $("#changeDays").val(da.changeDays);
                  }
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
	
}


function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}


 function save(){
 	var restDays=$("#restDays").val();
 	var dailyTasks=$("#dailyTasks").val();
 	var orderTicket=$("#orderTicket").val();
 	var worktimeMon=$("#worktimeMon").val();
 	var worktimeEve=$("#worktimeEve").val();
 	var changeDays=$("#changeDays").val();
 	//获得地区信息
	var code = $("#change").find("option:selected").val();
	//判断是否已存在
	if(restDays==""||worktimeMon==""||dailyTasks==""||orderTicket==""||worktimeEve==""){
		alert("输入不能为空！");
		return;
	}
	

	var reg = /^([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9])-([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9])?$/;
	if(reg.test(worktimeMon)==false){
		alert("早班时间格式不正确！");
		return;
	}
	if(reg.test(worktimeEve)==false){
		alert("早班时间格式不正确！");
		return;
	}
	

		$.ajax({
            url: urlcore + "/api/sendTask/checkSendTaskDetail",
            type: "post",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            data:JSON.stringify({
            	areaCode:code,
            }),
            success:function(data){
                if (data.success == true) {
                 saveAreaDetail(restDays,dailyTasks,orderTicket,worktimeMon,worktimeEve,code,changeDays);
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    if(confirm(data.msg)){
                    	updateAreaDetail(restDays,dailyTasks,orderTicket,worktimeMon,worktimeEve,code,changeDays);
                    }
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
 	
 	
 }
 
 //保存
function  saveAreaDetail(restDays,dailyTasks,orderTicket,worktimeMon,worktimeEve,code,changeDays){
		$.ajax({
            url: urlcore + "/api/sendTask/saveSendTask",
            type: "post",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            data:JSON.stringify({
            	restDays:restDays,
            	dailyTasks:dailyTasks,
            	orderTicket:orderTicket,
            	worktimeMon:worktimeMon,
            	worktimeEve:worktimeEve,
            	changeDays:changeDays,
            	areaCode:code
            }),
            success:function(data){
                if (data.success == true) {
                  alert("保存成功");
                  $("#areaCode").val(code);
                   location.reload();
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
	
}

//更新
function  updateAreaDetail(restDays,dailyTasks,orderTicket,worktimeMon,worktimeEve,code,changeDays){
		$.ajax({
            url: urlcore + "/api/sendTask/updateSendTaskDetail",
            type: "post",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            data:JSON.stringify({
            	restDays:restDays,
            	dailyTasks:dailyTasks,
            	orderTicket:orderTicket,
            	worktimeMon:worktimeMon,
            	worktimeEve:worktimeEve,          
            	changeDays:changeDays,
            	areaCode:code
            }),
            success:function(data){
                if (data.success == true) {
                  alert("更新成功");
                  $("#areaCode").val(code);
                  location.reload();
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
	
}

